﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ControlsLib;
using GestionePolveriLib.Facades;
using GestionePolveriLib.BL;
using BrightIdeasSoftware;
using System.Diagnostics;
using System.Collections;

namespace GestionePolveri2
{
    public partial class ComposizioneColoreManager2 : ComposizioneColoreBase2
    {
        public ComposizioneColoreManager2()
        {
            InitializeComponent();

            if (!this.DesignMode)
            {
                this.CanModify = true;
                this.ButtonClicked += new EventHandler<CustomEventArgs<ComposizioneColoreExt>>(Anagrafica_ButtonClicked);
                this.ItemSelected += new EventHandler<CustomEventArgs<ComposizioneColoreExt>>(Anagrafica_ItemSelected);

                LoadColori();

                LoadPolveri();

                LoadData(true);
            }
        }

        private void LoadColori()
        {
            ColoriFacade fcf = new ColoriFacade();
            fcf.LoadAsync((result) =>
            {
                this.objectListViewColori.SetObjects((List<Colore>)result);
            });
        }

        private void LoadPolveri()
        {
            PolvereFacade fcf = new PolvereFacade();
            fcf.LoadAsync((result) =>
            {
                List<PolvereExt> pexts = new List<PolvereExt>();
                foreach (Polvere c in (List<Polvere>)result)
                {
                    PolvereExt pExt = new PolvereExt(c);
                    pexts.Add(pExt);
                }

                this.objectListViewPolveri.AddDecoration(new EditingCellBorderDecoration { UseLightbox = true });
                this.objectListViewPolveri.SetObjects(pexts);
            });
        }

        private void LoadDataExt(bool selectLast)
        {
            ComposizioneColoreFacadeExt pf = new ComposizioneColoreFacadeExt();

            pf.LoadAsync((result) =>
            {
                this.dataGridView1.DataSource = result;



                //DataGridViewButtonColumn btnColumnPolveri = new DataGridViewButtonColumn();
                //btnColumnPolveri.DisplayIndex = 4;
                //btnColumnPolveri.Width = 100;
                //btnColumnPolveri.HeaderText = "Polveri Associate";
                //btnColumnPolveri.Text = "Polveri Associate";
                //this.dataGridView1.Columns.Add(btnColumnPolveri);

                //this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;

                //if (selectLast)
                //{
                //    if ((this.dataGridView1.Rows != null) && (this.dataGridView1.Rows.Count > 0))
                //    {
                //        this.dataGridView1.Rows[0].Selected = true;
                //        //this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].Selected = true;

                //        Anagrafica_ItemSelected(this.dataGridView1, new CustomEventArgs<ComposizioneColoreExt>(Actions.Unselected, (ComposizioneColoreExt)this.dataGridView1.Rows[0].DataBoundItem));
                //    }
                //}
            });
        }

        private void LoadData(bool selectLast)
        {
            ComposizioneColoreFacade pf = new ComposizioneColoreFacade();

            pf.LoadAsync((result) =>
            {
                this.dataGridView1.DataSource = result;

                List<ComposizioneColore> pexts = new List<ComposizioneColore>();
                foreach (ComposizioneColore c in (List<ComposizioneColore>)result)
                {
                    pexts.Add(c);
                }

                this.objectListViewElenco.AddDecoration(new EditingCellBorderDecoration { UseLightbox = true });
                this.objectListViewElenco.SetObjects(pexts);




                //DataGridViewButtonColumn btnColumnPolveri = new DataGridViewButtonColumn();
                //btnColumnPolveri.DisplayIndex = 4;
                //btnColumnPolveri.Width = 100;
                //btnColumnPolveri.HeaderText = "Polveri Associate";
                //btnColumnPolveri.Text = "Polveri Associate";
                //this.dataGridView1.Columns.Add(btnColumnPolveri);

                //this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;

                //if (selectLast)
                //{
                //    if ((this.dataGridView1.Rows != null) && (this.dataGridView1.Rows.Count > 0))
                //    {
                //        this.dataGridView1.Rows[0].Selected = true;
                //        //this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].Selected = true;

                //        //Anagrafica_ItemSelected(this.dataGridView1, new CustomEventArgs<ComposizioneColore>(Actions.Unselected, (ComposizioneColore)this.dataGridView1.Rows[0].DataBoundItem));
                //    }
                //}
            });
        }

        void Anagrafica_ButtonClicked(object sender, CustomEventArgs<ComposizioneColoreExt> e)
        {
            ComposizioneColoreFacadeExt impf = new ComposizioneColoreFacadeExt();
            ComposizioneColoreExt data = new ComposizioneColoreExt();

            switch (e.Action)
            {
                case Actions.New:
                    base.ClearControls(this.Controls);
                    break;
                case Actions.Add:
                    {
                        Colore colore = (Colore)this.objectListViewColori.CheckedObject;

                        IList a = this.objectListViewPolveri.CheckedObjects;

                        List<PolvereExt> polveri = new List<PolvereExt>();
                        foreach (PolvereExt value in this.objectListViewPolveri.CheckedObjects) polveri.Add(value);

                        //data.Colore = (Colore)this.dynCmbColori.SelectedValue;
                        //data.Polvere = (Polvere)this.dynCmbPolveri.SelectedValue;
                        //data.Applicazione = this.dynApplicazioneTextBox.Text;

                        data.Colore = colore;
                        data.Polveri = polveri;


                        impf.Add(data);

                        //LoadData(false);
                    }
                    break;
                case Actions.Modify:
                    {
                        data.ID = base.Current.ID;
                        //data.Colore = (Colore)this.dynCmbColori.SelectedValue;
                        //data.Polvere = (Polvere)this.dynCmbPolveri.SelectedValue;
                        //data.Applicazione = this.dynApplicazioneTextBox.Text;

                        impf.Modify(data);

                        //LoadData(false);
                    }
                    break;
                case Actions.Delete:
                    {
                        data.ID = base.Current.ID;

                        impf.Delete(data);

                        //LoadData(false);
                    }
                    break;
                default:
                    break;
            }
        }

        void Anagrafica_ItemSelected(object sender, CustomEventArgs<ComposizioneColoreExt> e)
        {
            base.Current = e.Item;


        }

        private void dynFindColore_TextChanged(object sender, EventArgs e)
        {
            TextMatchFilter filtroColore = TextMatchFilter.Contains(this.objectListViewColori, dynFindColore.Text);
            filtroColore.Columns = new[] { this.olvCodiceColore, this.olvDescrizioneFamiglia };
            this.objectListViewColori.ModelFilter = filtroColore;
            
            //this.objectListViewColori.ModelFilter = TextMatchFilter.Prefix(this.objectListViewColori, this.dynFindColore.Text);
        }

        private void dynFindPolvere_TextChanged(object sender, EventArgs e)
        {
            TextMatchFilter filtroPolvere = TextMatchFilter.Contains(this.objectListViewPolveri, dynFindPolvere.Text);
            filtroPolvere.Columns = new[] { this.olvCodicePolvere, this.olvDescrizioneFornitore };
            this.objectListViewPolveri.ModelFilter = filtroPolvere;

            //this.objectListViewPolveri.ModelFilter = TextMatchFilter.Prefix(this.objectListViewPolveri, this.dynFindPolvere.Text);
        }

        // colori associati
        //ComposizioneColoreFacade ccf = new ComposizioneColoreFacade();
        //List<Colore> data = ccf.GetColoriAssociati(((Polvere)this.dynCmbPolveri.SelectedValue).ID);
        // polveri associate
        //ComposizioneColoreFacade ccf = new ComposizioneColoreFacade();
        //List<Polvere> data = ccf.GetPolveriAssociate(((Colore)this.dynCmbColori.SelectedValue).ID);
    }

    public class ComposizioneColoreBase2 : AnagraficaManager<ComposizioneColoreExt> { }
}
